<?php
/**
 * 用户相关信息
 * User: Acer
 * Date: 2022/7/10
 * Time: 10:46
 */

namespace app\move\controller;

use app\admin\service\UserServide;
use app\core\MyController as MyController;
use app\move\service\CardUserService;
use app\move\service\EquityService;
use app\move\service\WxUserService;
use app\wx\service\AuthorizeService;

class User extends MyController
{
    protected $noNeedAction = []; // 不需要校验控制器，通道
    // 操作前置
    protected $beforeActionList = ['my_initialize'];

    // 操作前置方法
    public function my_initialize()
    {
    }

    // 用户本人身份实名认证
    public function user_authentication(){
        $data = input();
        if(!isset($data['name']) || !isset($data['people_card_code']))  json_err( -201);
        $UserServide = new WxUserService();
        $data['uid'] = $this->userId;
        $is_authentication = $UserServide->is_user_authentication($data['uid']);
        if($is_authentication['id_card']) json_err(-210,['该用户已认证']);
        // 实名认证
        $UserServide->s_user_authentication($data);
        // 根据uid获取用户信息
        $user = (new AuthorizeService())->s_getWxUserIdInfo($data['uid']);
        if(!$user) json_err(-210,['用户信息获取失败！']);
        // 根据user_id 生成token
        $token = $this->create_token($user['id'],$this->expire);
        $user['token'] = $token;
        $user['is_authorize'] = 0;// 是否实名认证（1：是、0：否）
        if(isset($user['idCardInfo']) && isset($user['idCardInfo']['mobile']) && isset($user['idCardInfo']['id_card'])){
            $id_card = $user['idCardInfo']['id_card'];
            $mobile = $user['idCardInfo']['mobile'];
            if(!empty($id_card) && !empty($mobile) &&!empty($user['did'])) $user['is_authorize'] = 1;
        }
        $user['token_invalid']= time()+ ($this->expire - 10);
        _action_result(200, $user, '操作成功！');
    }

    // 添加用户亲人信息
    public function add_user_family(){
        $data = input();
        if(!isset($data['type']) || !isset($data['pid'])  || !isset($data['name']) || !isset($data['people_card_code']) )  json_err( -201);
        $UserServide = new WxUserService();
        $data['uid'] = $this->userId;
        $res = $UserServide->s_add_user_family($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 修改用户亲人信息
    public function edit_user_family(){
        $data = input();
        if(!isset($data['did']) || !isset($data['type']) || !isset($data['pid'])  || !isset($data['name']) || !isset($data['people_card_code']) )  json_err( -201);
        $UserServide = new WxUserService();
        $data['uid'] = $this->userId;
        $res = $UserServide->s_edit_id_card_info($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }


}
